﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLNS_Client
{
    public partial class UC_CanBo_TDCM : UserControl
    {
        string temp = "";
        string temp1 = "";
        string flag = "";
        ServiceReference1.Service_QLNSClient sv = new ServiceReference1.Service_QLNSClient();
        public UC_CanBo_TDCM()
        {
            InitializeComponent();
        }

        private void btn_ThemMaTDCM_CB_TDCM_Click(object sender, EventArgs e)
        {
            frm_TDCM themtdcm = new frm_TDCM();
            themtdcm.ShowDialog();
        }

        private void UC_CanBo_TDCM_Load(object sender, EventArgs e)
        {
            Load_CB_TDCM();

            //Hiển thị tùy chọn trình độ chuyên môn
            cmb_MaTDCM_TDCM.DataSource = sv.getTDCM();
            cmb_MaTDCM_TDCM.DisplayMember = "TenTDCM";
            cmb_MaTDCM_TDCM.ValueMember = "Ma_TD_CM";

            cmb_HinhThuc_DT_DTCM.DataSource = sv.getCB_TDCM();
            cmb_HinhThuc_DT_DTCM.DisplayMember = "HinhThucDT";
            //cmb_HinhThuc_DT_DTCM.ValueMember = "Ma_TD_CM";
            //cmb_HinhThuc_DT_DTCM.ValueMember = "Ma_CB";

            cmb_MaCB_TDCM.DataSource = sv.getCB_TDCM();
            cmb_MaCB_TDCM.DisplayMember = "Ma_CB";
            cmb_MaCB_TDCM.ValueMember = "Ma_CB";
        }
        public void Load_CB_TDCM()
        {
            dataGridView_TDCM.DataSource = sv.getCB_TDCM();
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
        }

        private void dataGridView_TDCM_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            cmb_MaCB_TDCM.Text = dataGridView_TDCM.CurrentRow.Cells[0].Value.ToString();
            cmb_MaTDCM_TDCM.Text = dataGridView_TDCM.CurrentRow.Cells[1].Value.ToString();
            txt_TenTruong_TDCM.Text = dataGridView_TDCM.CurrentRow.Cells[2].Value.ToString();
            txt_ChuyenNganh_DT_TDCM.Text = dataGridView_TDCM.CurrentRow.Cells[3].Value.ToString();
            date_NamBD_TDCM.Text = dataGridView_TDCM.CurrentRow.Cells[4].Value.ToString();
            date_NamKT_TDCM.Text = dataGridView_TDCM.CurrentRow.Cells[5].Value.ToString();
            cmb_HinhThuc_DT_DTCM.Text = dataGridView_TDCM.CurrentRow.Cells[6].Value.ToString();
            temp = dataGridView_TDCM.CurrentRow.Cells[0].Value.ToString();
            temp1 = dataGridView_TDCM.CurrentRow.Cells[1].Value.ToString();
            cmb_MaCB_TDCM.Enabled = false;
            cmb_MaTDCM_TDCM.Enabled = false;
            date_NamBD_TDCM.Enabled = false;
            date_NamKT_TDCM.Enabled = false;
            txt_ChuyenNganh_DT_TDCM.Enabled = false;
            txt_TenTruong_TDCM.Enabled = false;
            cmb_HinhThuc_DT_DTCM.Enabled = false;
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            flag = "them";
            btn_Luu.Enabled = true;
            btn_Huy.Enabled = true;
            btn_Sua.Enabled = false;
            btb_Xoa.Enabled = false;
            cmb_MaCB_TDCM.Enabled = true;
            cmb_MaTDCM_TDCM.Enabled = true;
            date_NamBD_TDCM.Enabled = true;
            date_NamKT_TDCM.Enabled = true;
            txt_ChuyenNganh_DT_TDCM.Enabled = true;
            txt_TenTruong_TDCM.Enabled = true;
            cmb_HinhThuc_DT_DTCM.Enabled = true;
            cmb_MaCB_TDCM.Text = "";
            cmb_MaTDCM_TDCM.Text = "";
            date_NamBD_TDCM.Text = "";
            date_NamKT_TDCM.Text = "";
            txt_ChuyenNganh_DT_TDCM.Text = "";
            txt_TenTruong_TDCM.Text = "";
            cmb_HinhThuc_DT_DTCM.Text = "";
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = true;
            btb_Xoa.Enabled = true;
            btn_Sua.Enabled = true;

            ServiceReference1.CB_TDCM_QUATRINH_DT cbtdcm = new ServiceReference1.CB_TDCM_QUATRINH_DT();
            string a = cmb_MaCB_TDCM.Text;
            string b = cmb_MaTDCM_TDCM.SelectedValue.ToString();
            string c = txt_TenTruong_TDCM.Text;
            string h = txt_ChuyenNganh_DT_TDCM.Text;
            string d = date_NamBD_TDCM.Text;
            string m = date_NamKT_TDCM.Text;
            string k = cmb_HinhThuc_DT_DTCM.Text;
            
            cbtdcm.Ma_CB = a;
            cbtdcm.Ma_TD_CM = b;
            cbtdcm.TenTruong = c;
            cbtdcm.ChuyenNganhDT = h;
            cbtdcm.NamBatDau = Convert.ToDateTime(d);
            cbtdcm.NamKetThuc = Convert.ToDateTime(m);
            cbtdcm.HinhThucDT = k;

            if (a == "" | b == "" | c == "" | d == "" | m == "" | k == "" | h =="")
            {
                MessageBox.Show("Xin nhập đầy đủ dữ liệu!");
            }
            else
            {
                int kq = 0;
                if (flag == "them")
                {
                    if (sv.checkCB_TDCM(cbtdcm))
                        kq = -1;
                    else
                        sv.addCB_TDCM(cbtdcm);
                }
                else
                {
                    if (flag == "sua")
                    {
                        sv.setCB_TDCM(cbtdcm);
                        kq = 1;
                    }
                }
                if (kq != -1)
                {
                    MessageBox.Show("Hoàn tất!");
                    Load_CB_TDCM();
                    btb_Xoa.Enabled = true;
                    btn_Sua.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Lỗi!!!");
                }
            }
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
            btn_Sua.Enabled = true;
            btb_Xoa.Enabled = true;
        }

        private void btb_Xoa_Click(object sender, EventArgs e)
        {
            ServiceReference1.CB_TDCM_QUATRINH_DT cbtdcm = new ServiceReference1.CB_TDCM_QUATRINH_DT();
            cbtdcm.Ma_CB = temp;
            cbtdcm.Ma_TD_CM = temp1;
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn xóa dữ liệu này?", "warnring", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                sv.deleteCB_TDCM(cbtdcm);
                Load_CB_TDCM();
                MessageBox.Show("Đã xóa!");
                cmb_MaCB_TDCM.Text = "";
                cmb_MaTDCM_TDCM.Text = "";
                txt_TenTruong_TDCM.Text = "";
                txt_ChuyenNganh_DT_TDCM.Text = "";
                date_NamBD_TDCM.Text = "";
                date_NamKT_TDCM.Text = "";
                cmb_HinhThuc_DT_DTCM.Text = "";
            }
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            flag = "sua";
            btn_Luu.Enabled = true;
            btn_Huy.Enabled = true;
            txt_TenTruong_TDCM.Enabled = true;
            txt_ChuyenNganh_DT_TDCM.Enabled = true;
            date_NamBD_TDCM.Enabled = true;
            date_NamKT_TDCM.Enabled = true;
            cmb_HinhThuc_DT_DTCM.Enabled = true;

        }
    }
}
